﻿var ReportTimerID;
var IsAuto = false;


function ddl() {
    if ($("#ReportFilters1_optRegionMarket")[0].checked)
        return "#ReportFilters1_ddlManagers"
    else
        return "#ReportFilters1_ddlMarkets"
}

$(function () {

    $("#ReportFilters1_btnRunReport").click(function (evt) {

        var btnRunReport = $("#ReportFilters1_btnRunReport")[0]
        var txtFrom = $("#ReportFilters1_txtFrom")[0]
        var txtTo = $("#ReportFilters1_txtTo")[0]
        var ddlMarkets = $("#ReportFilters1_ddlMarkets", parent.frames[1].document.forms[0])[0]
        var ddlManagers = $("#ReportFilters1_ddlManagers", parent.frames[1].document.forms[0])[0]
        var chlLocations = $("#ReportFilters1_chlLocations", parent.frames[1].document.forms[0])[0]
        var chlDwellings = $("#ReportFilters1_chlDwellings", parent.frames[1].document.forms[0])[0]
        var chlProducts = $("#ReportFilters1_chlProducts", parent.frames[1].document.forms[0])[0]
        var chlProducts2 = $("#ReportFilters1_chlProducts2", parent.frames[1].document.forms[0])[0]
        var optRegionMarket = $("#ReportFilters1_optRegionMarket", parent.frames[1].document.forms[0])[0]
        var optAcctManager = $("#ReportFilters1_optAcctManager", parent.frames[1].document.forms[0])[0]

        btnRunReport.disabled = true;
        txtFrom.disabled = true;
        txtTo.disabled = true;
        optRegionMarket.disabled = true;
        ddlMarkets.disabled = true;
        optAcctManager.disabled = true;
        ddlManagers.disabled = true;
        chlLocations.disabled = true;
        chlDwellings.disabled = true;
        chlProducts.disabled = true;
        chlProducts2.disabled = true;

        var days = 30
        $("#imgLoadingReport").css("display", "")

        setReportCookie("FromDate", txtFrom.value, days)
        setReportCookie("ToDate", txtTo.value, days)

        setReportCookie("RegionMarket", $.trim(ddlMarkets.value), days)
        setReportCookie("IsRegionMarket", optRegionMarket.checked, days)

        setReportCookie("AcctMgr", $.trim(ddlManagers.value), days)
        setReportCookie("IsAcctMgr", optAcctManager.checked, days)

        setReportCookie("National",
        chlLocations.cells[0].firstChild.checked ? chlLocations.cells[0].firstChild.value :
        chlLocations.cells[1].firstChild.checked ? chlLocations.cells[1].firstChild.value :
        chlLocations.cells[2].firstChild.value, days)

        setReportCookie("Dwelling",
        chlDwellings.cells[0].firstChild.checked ? chlDwellings.cells[0].firstChild.value :
        chlDwellings.cells[1].firstChild.checked ? chlDwellings.cells[1].firstChild.value :
        chlDwellings.cells[2].firstChild.value, days)

        setReportCookie("LineOfBusiness",
        chlProducts.cells[0].firstChild.checked ? chlProducts.cells[0].firstChild.value :
        chlProducts.cells[1].firstChild.checked ? chlProducts.cells[1].firstChild.value :
        chlProducts.cells[2].firstChild.checked ? chlProducts.cells[2].firstChild.value :
        chlProducts.cells[3].firstChild.value, days)

        if (chlProducts2 != null) setReportCookie("ByProductOrChannel",
        chlProducts2.cells[0].firstChild.checked ? chlProducts2.cells[0].firstChild.value : chlProducts2.cells[1].firstChild.value, days)


        if ($("#FirstLevelList")[0].selectedIndex > 0) {
            parent.reports.location = $("#FirstLevelList")[0].value + "&Title=" + $("#FirstLevelList option:selected")[0].text
        }

        if (txtFrom.value != null) setReportCookie(txtFrom.id, txtFrom.value, days)
        if (txtTo.value != null) setReportCookie(txtTo.id, txtTo.value, days)

        if (optRegionMarket.checked != null) setReportCookie(optRegionMarket.id, optRegionMarket.checked, days)
        if (ddlMarkets.selectedIndex != null) setReportCookie(ddlMarkets.id, ddlMarkets.selectedIndex, days)

        if (optAcctManager.checked != null) setReportCookie(optAcctManager.id, optAcctManager.checked, days)
        if (ddlManagers.selectedIndex != null) setReportCookie(ddlManagers.id, ddlManagers.selectedIndex, days)

        if (chlLocations != null) setReportCookie(chlLocations.id,
        chlLocations.cells[0].firstChild.checked ? 0 :
        chlLocations.cells[1].firstChild.checked ? 1 :
        2, days)

        if (chlDwellings != null) setReportCookie(chlDwellings.id,
        chlDwellings.cells[0].firstChild.checked ? 0 :
        chlDwellings.cells[1].firstChild.checked ? 1 :
        2, days)

        if (chlProducts != null) setReportCookie(chlProducts.id,
        chlProducts.cells[0].firstChild.checked ? 00 :
        chlProducts.cells[1].firstChild.checked ? 1 :
        chlProducts.cells[2].firstChild.checked ? 2 :
        3, days)

        if (chlProducts2 != null) setReportCookie(chlProducts2.id,
        chlProducts2.cells[0].firstChild.checked ? 0 : 1, days)


    });


    //    $("#ReportFilters1_optAcctManager").click(function (evt) {
    //        $("#ReportFilters1_ddlManagers")[0].disabled = !$("#ReportFilters1_optAcctManager")[0].checked;
    //        $("#ReportFilters1_ddlMarkets")[0].disabled = !$("#ReportFilters1_ddlManagers")[0].disabled;
    //    });

    //    $("#ReportFilters1_optRegionMarket").click(function (evt) {
    //        $("#ReportFilters1_ddlMarkets")[0].disabled = !$("#ReportFilters1_optRegionMarket")[0].checked;
    //        $("#ReportFilters1_ddlManagers")[0].disabled = !$("#ReportFilters1_ddlMarkets")[0].disabled;
    //    });

    $("#ReportFilters1_ddlManagers").change(function () {
        $("#ReportFilters1_optAcctManager")[0].checked = true;
    });

    $("#ReportFilters1_ddlMarkets").change(function () {
        $("#ReportFilters1_optRegionMarket")[0].checked = true;
    });

    function setReportCookie(c_name, value, expiredays) {
        c_name += "_Report"
        c_name = c_name.replace("ReportFilters1_", "")
        var exdate = new Date();
        exdate.setDate(exdate.getDate() + 30);
        document.cookie = c_name + "=" + escape(value) + ";expires=" + exdate.toUTCString();
    }

    function getReportCookie(c_name) {
        c_name += "_Report"
        if (document.cookie.length > 0) {
            c_start = document.cookie.indexOf(c_name + "=");
            if (c_start != -1) {
                c_start = c_start + c_name.length + 1;
                c_end = document.cookie.indexOf(";", c_start);
                if (c_end == -1) c_end = document.cookie.length;
                return unescape(document.cookie.substring(c_start, c_end));
            }
        }
        return "";
    }


});

//    ReportTimerID = setInterval(function () {
//        ClickNext()
//    }, GetTimeBetween() * 1000);


//    if ($("#ReportFilters1_txtSpeed").length) $("#ReportFilters1_txtSpeed")[0].value = GetTimeBetween()
//    ColorLamp("")

//    $(".pushbutton").click(function () {
//        $(this).css("position", "relative");
//        $(this).css("left", "1px");
//        $(this).css("top", "1px");
//    });

//    //    $("#ReportFilters1_ddlRegions").change(function () {

//    //        if ($("#ReportFilters1_ddlRegions")[0].selectedIndex == 0) $("#ReportFilters1_ddlRegions")[0].size = 0;

//    //        var nDirection = parseInt(getReportCookie("Direction"))
//    //        $(this).hide("slow");
//    //        $("#ReportFilters1_imgbtnRefresh1").hide("slow");
//    //        $("#ReportFilters1_imgLoadingFirst").css("display", "");
//    //        $("#ReportFilters1_Label1")[0].innerText = $("#ReportFilters1_ddlRegions" + " :selected").text() + "..."
//    //       
//    //    });


//    //    $("#ReportFilters1_imgbtnRefresh1").click(function () {
//    //        $(this).hide("slow");
//    //        $("#ReportFilters1_ddlRegions").hide("slow");
//    //        $("#ReportFilters1_imgLoadingFirst").css("display", "");
//    //        $("#ReportFilters1_Label1")[0].innerText = $("#ReportFilters1_ddlRegions" + " :selected").text() + "..."

//    //    });

//    //var "#ReportFilters1_ddlRegions" = "#ReportFilters1_SecondLevelList"
//    $("#ReportFilters1_lnkbtnNext").click(function (evt) {

//        var txt = $(ddl() + " :selected").text()

//        if ($(ddl())[0].selectedIndex + 1 < $(ddl())[0].length) {
//            do {
//                $(ddl())[0].selectedIndex += 1
//            }
//            while (txt == $(ddl() + " :selected").text())
//        }
//        else
//            $(ddl())[0].selectedIndex = 1

//        $(ddl()).change()
//        $("#ReportFilters1_btnRunReport").click()
//        evt.preventDefault()
//    });

//    $("#ReportFilters1_lnkbtnPrev").click(function (evt) {

//        var txt = $(ddl() + " :selected").text()

//        if ($(ddl())[0].selectedIndex - 1 > 0) {
//            $(ddl())[0].selectedIndex -= 1
//            var txtNext = $(ddl() + " :selected").text()
//            if (txt == txtNext)
//                if ($(ddl())[0].selectedIndex - 2 > 0)
//                    $(ddl())[0].selectedIndex -= 2

//            }
//            else
//                $(ddl())[0].selectedIndex = $(ddl())[0].length - 1

//            $(ddl()).change()
//            evt.preventDefault()
//        });

//        $("#ReportFilters1_lnkbtnPause").click(function (evt) {
//            //setReportCookie("Direction", 0, 1)
//            if (ReportTimerID != null) clearInterval(ReportTimerID)
//            ColorLamp("yellow")
//            evt.preventDefault()
//        });


//        $("#ReportFilters1_lnkbtnForward").click(function (evt) {
//            ColorLamp("green")
//            setReportCookie("Direction", 1, 1)
//            ReportTimerID = setInterval(function () {
//                ClickNext()
//            }, GetTimeBetween() * 1000)
//            evt.preventDefault()
//        });

//        $("#ReportFilters1_lnkbtnBack").click(function (evt) {
//            setReportCookie("Direction", -1, 1)
//            ColorLamp("green")
//            ReportTimerID = setInterval(function () {
//                ClickNext()
//            }, GetTimeBetween() * 1000)
//            evt.preventDefault()
//        });

//        $("#ReportFilters1_lnkbtnLast").click(function (evt) {
//            ColorLamp("blue")
//            setReportCookie("Direction", 0, 1)
//            $(ddl())[0].selectedIndex = $(ddl())[0].length - 1
//            $(ddl()).change()
//            evt.preventDefault()
//        });

//        $("#ReportFilters1_lnkbtnStop").click(function (evt) {
//            //setReportCookie("Direction", 0, 1)
//            if (ReportTimerID != null) clearInterval(ReportTimerID)
//            ColorLamp("red")
//            evt.preventDefault()
//        });

//        $("#ReportFilters1_lnkbtnSlower").click(function (evt) {
//            var nMillisecs = parseInt(GetTimeBetween() * 1000)
//            if (nMillisecs < 60000) nMillisecs += 1000
//            setReportCookie("TimeBetween", nMillisecs / 1000, 30)
//            clearInterval(ReportTimerID)
//            ReportTimerID = setInterval(function () {
//                ClickNext()
//            }, GetTimeBetween() * 1000);

//            evt.preventDefault()
//            $("#ReportFilters1_txtSpeed")[0].value = parseInt(getReportCookie("TimeBetween"))
//        });

//        $("#ReportFilters1_lnkbtnFaster").click(function (evt) {
//            var nMillisecs = parseInt(GetTimeBetween() * 1000)
//            if (nMillisecs > 1000) nMillisecs -= 1000
//            setReportCookie("TimeBetween", nMillisecs / 1000, 30)
//            clearInterval(ReportTimerID)
//            ReportTimerID = setInterval(function () {
//                ClickNext()
//            }, GetTimeBetween() * 1000);
//            evt.preventDefault()
//            $("#ReportFilters1_txtSpeed")[0].value = parseInt(getReportCookie("TimeBetween"))
//        });

//        $("#ReportFilters1_lnkbtnFirst").click(function (evt) {
//            ColorLamp("blue")
//            $(ddl())[0].selectedIndex = 1
//            $(ddl()).change()
//            evt.preventDefault()
//        });


//    function GetTimeBetween() {

//        var nSecs = parseInt(getReportCookie("TimeBetween"))
//        if (!nSecs || nSecs > 60 || nSecs < 1) nSecs = 10

//        return nSecs
//    }

//    function ColorLamp(sColor) {
//        if (sColor == "")
//            sColor = getReportCookie("LampColor");
//        else
//            setReportCookie("LampColor", sColor, 1);
//        if (sColor == "") { sColor = "blue"; setReportCookie("LampColor", sColor, 1) }
//        x = $("#ReportFilters1_imgNowDoing")
//        if (x.length > 0) {
//            var src = $("#ReportFilters1_imgNowDoing")[0].attributes["src"].nodeValue
//            var saSrc = src.split("/")
//            var n = saSrc.length - 1
//            saSrc[n] = "led-" + sColor + ".gif"
//            var newSrc = saSrc.join("/")
//            $("#ReportFilters1_imgNowDoing")[0].attributes["src"].nodeValue = newSrc
//        }

//    }


//    function ClickNext() {
//        var nDirection = parseInt(getReportCookie("Direction"))
//        var sColor = getReportCookie("LampColor")

//        if (nDirection == 1 && sColor == "green")
//            $("#ReportFilters1_lnkbtnNext").click()
//        else
//            if (nDirection == -1 && sColor == "green")
//                $("#ReportFilters1_lnkbtnPrev").click()
//            else
//                if (sColor != "green") clearInterval(ReportTimerID)

//    }
